{% load static humanize %}
{% load param_replace %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智淘go - 二手交易平台</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: rgba(255, 107, 107, 0.3);
            --secondary: #4ECDC4;
            --text-dark: #2D3436;
            --text-light: #636E72;
            --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }

        body {
            background: url('https://pic2.zhimg.com/v2-86ffa1ad12c00587d5cb6f9bd362578a_r.jpg?source=1940ef5c') no-repeat center center fixed;
            font-family: 'Helvetica Neue', Arial, 'Microsoft YaHei', sans-serif;
            background-color: #f8fafc;
        }

        .filter-container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 1rem;
        }

        .search-box {
            position: relative;
            margin-bottom: 2rem;
        }

        .search-input {
            width: 100%;
            padding: 1rem 2rem;
            border-radius: 30px;
            border: none;
            font-size: 1.1rem;
            box-shadow: var(--shadow);
            transition: all 0.3s ease;
        }

        .search-input:focus {
            outline: none;
            box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.3);
        }

        .search-btn {
            position: absolute;
            right: 1.5rem;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: var(--primary);
            font-size: 1.2rem;
            cursor: pointer;
        }

        .filter-sidebar {
            background: white;
            padding: 2rem;
            border-radius: 16px;
            box-shadow: var(--shadow);
            margin-bottom: 2rem;
        }

        .filter-section {
            margin-bottom: 2rem;
            padding-bottom: 1.5rem;
            border-bottom: 1px solid #eee;
        }

        .filter-section h3 {
            color: var(--text-dark);
            margin-bottom: 1rem;
            font-size: 1.1rem;
        }

        .price-inputs {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 1rem;
        }

        .price-input {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            font-size: 1rem;
            padding-right: 2rem;
        }

        .price-symbol {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-light);
            pointer-events: none;
        }

        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 2rem;
            padding: 2rem 0;
            max-width: 1440px;
            margin: 0 auto;
        }

        .product-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: var(--shadow);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            position: relative;
        }

        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }

        .product-image {
            width: 100%;
            height: 240px;
            object-fit: cover;
            border-bottom: 1px solid #f1f5f9;
        }

        .product-badge {
            position: absolute;
            top: 15px;
            left: 15px;
            background: var(--primary);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 20px;
            font-size: 0.9rem;
            font-weight: 500;
        }

        .sold-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.6);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            font-weight: bold;
        }

        .product-info {
            padding: 1.5rem;
        }

        .product-title {
            font-size: 1.2rem;
            color: var(--text-dark);
            margin-bottom: 0.8rem;
            line-height: 1.4;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .product-price {
            color: var(--primary);
            font-size: 1.5rem;
            font-weight: 700;
            margin: 1rem 0;
        }

        .product-meta {
            display: flex;
            justify-content: space-between;
            color: var(--text-light);
            font-size: 0.9rem;
            margin-top: 1rem;
        }

        .pagination {
            display: flex;
            justify-content: center;
            gap: 0.5rem;
            margin: 3rem 0;
        }

        .page-link {
            padding: 0.75rem 1.25rem;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            color: var(--text-dark);
            text-decoration: none;
            transition: all 0.2s ease;
        }

        .page-link:hover {
            background: var(--primary);
            color: white;
            border-color: var(--primary);
        }

        .page-link.active {
            background: var(--primary);
            color: white;
            border-color: var(--primary);
        }

        .no-results {
            text-align: center;
            padding: 4rem 0;
            grid-column: 1 / -1;
        }

        .no-results h3 {
            color: var(--text-dark);
            margin-bottom: 1rem;
        }

        .search-stats {
            display: flex;
            flex-direction: column;
            gap: 1rem;
            margin-bottom: 2rem;
            padding: 1rem;
            background: white;
            border-radius: 8px;
            box-shadow: var(--shadow);
        }

        .search-stats span {
            font-size: 1.1rem;
            color: var(--text-dark);
            font-weight: 500;
        }

        .search-tips {
            display: flex;
            gap: 1rem;
            flex-wrap: wrap;
        }

        .search-tips a {
            padding: 0.5rem 1rem;
            background: #f0f4f8;
            border-radius: 20px;
            color: var(--text-dark);
            text-decoration: none;
            transition: all 0.2s ease;
        }

        .search-tips a:hover {
            background: var(--primary);
            color: white;
        }
    </style>
</head>
<body>
    <header style="background: linear-gradient(135deg, var(--primary) 0%, #FF8E53 100%); padding: 2rem 0">
        <div class="filter-container">
            <form method="GET" action="{% url 'product_list' %}">
                <div class="search-box">
                    <input class="search-input"
                           type="text"
                           name="q"
                           placeholder="搜索商品名称或描述..."
                           value="{{ request.GET.q }}">
                    <button type="submit" class="search-btn">
                        <i class="fas fa-search"></i>
                    </button>
                </div>

                <div class="filter-sidebar">
                    <div class="filter-section">
                        <h3>商品分类</h3>
                        <select name="category" class="price-input">
                          <option value="">所有分类</option>
                          {% for cat in categories %}
                          <option value="{{ cat.0 }}"
                            {% if request_params.category == cat.0 %}selected{% endif %}>
                            {{ cat.1 }}
                          </option>
                          {% endfor %}
                        </select>
                    </div>

                    <div class="filter-section">
                        <h3>商品状态</h3>
                        <div style="display: grid; gap: 0.8rem;">
                            {% for cond in conditions %}
                            <label>
                              <input type="checkbox"
                                     name="conditions"
                                     value="{{ cond.0 }}"
                                     {% if cond.0 in selected_conditions %}checked{% endif %}>
                              {{ cond.1 }}
                            </label>
                            {% endfor %}
                        </div>
                    </div>

                    <div class="filter-section">
                        <h3>价格范围</h3>
                        <div class="price-inputs">
                            <div style="position: relative; width: 100%;">
                                <input type="number"
                                       name="min_price"
                                       class="price-input"
                                       placeholder="最低价"
                                       min="0"
                                       step="0.01"
                                       value="{{ request.GET.min_price }}">
                                <span class="price-symbol">¥</span>
                            </div>
                            <div style="position: relative; width: 100%;">
                                <input type="number"
                                       name="max_price"
                                       class="price-input"
                                       placeholder="最高价"
                                       min="0"
                                       step="0.01"
                                       value="{{ request.GET.max_price }}">
                                <span class="price-symbol">¥</span>
                            </div>
                        </div>
                    </div>

                    <div class="filter-section">
                        <h3>排序方式</h3>
                        <select name="sort" class="price-input">
                            <option value="-created_at" {% if request.GET.sort == "-created_at" %}selected{% endif %}>最新发布</option>
                            <option value="price" {% if request.GET.sort == "price" %}selected{% endif %}>价格从低到高</option>
                            <option value="-price" {% if request.GET.sort == "-price" %}selected{% endif %}>价格从高到低</option>
                            <option value="-views" {% if request.GET.sort == "-views" %}selected{% endif %}>人气优先</option>
                        </select>
                    </div>

                    <button type="submit"
                            style="width: 100%;
                                   padding: 1rem;
                                   background: var(--primary);
                                   color: white;
                                   border: none;
                                   border-radius: 8px;
                                   font-size: 1rem;
                                   cursor: pointer;
                                   transition: opacity 0.3s ease;"
                            onmouseover="this.style.opacity='0.9'"
                            onmouseout="this.style.opacity='1'">
                        应用筛选条件
                    </button>
                </div>
            </form>
        </div>
    </header>

    <main class="filter-container">
        <div class="search-stats">
            <span>找到 {{ products.paginator.count|intcomma }} 件商品</span>
            {% if request.GET.q %}
            <div class="search-tips">
                搜索建议：
                <a href="?{% param_replace request q=request.GET.q|add:' 配件' %}">{{ request.GET.q }} 配件</a>
                <a href="?{% param_replace request q=request.GET.q|add:' 套装' %}">{{ request.GET.q }} 套装</a>
            </div>
            {% endif %}
        </div>

        <div class="product-grid">
            {% for product in products %}
            <a href="{% url 'product_detail' product.id %}" class="product-card">
                {% if product.is_sold %}
                <div class="sold-overlay">已售出</div>
                {% endif %}

                <div class="product-badge">
                    {{ product.get_condition_display }}
                </div>

                <img src="{{ product.image.url }}"
                     class="product-image"
                     loading="lazy"
                     alt="{{ product.name }}">

                <div class="product-info">
                    <h3 class="product-title">{{ product.name }}</h3>
                    <div class="product-price">¥{{ product.price|intcomma }}</div>
                    <div class="product-meta">
                        <span><i class="fas fa-map-marker-alt"></i> {{ product.location }}</span>
                        <span><i class="fas fa-eye"></i> {{ product.views }}</span>
                    </div>
                </div>
            </a>
            {% empty %}
            <div class="no-results">
                <h3>暂时没有找到符合条件的商品</h3>
                <p>请尝试调整筛选条件</p>
            </div>
            {% endfor %}
        </div>

        {% if products.paginator.num_pages > 1 %}
        <div class="pagination">
            {% if products.has_previous %}
                <a href="?{% param_replace request page=1 %}" class="page-link">
                    <i class="fas fa-angle-double-left"></i>
                </a>
                <a href="?{% param_replace request page=products.previous_page_number %}" class="page-link">
                    <i class="fas fa-angle-left"></i>
                </a>
            {% endif %}

            {% for num in products.paginator.page_range %}
                {% if num == products.number %}
                    <span class="page-link active">{{ num }}</span>
                {% elif num > products.number|add:-3 and num < products.number|add:3 %}
                    <a href="?{% param_replace request page=num %}" class="page-link">{{ num }}</a>
                {% endif %}
            {% endfor %}

            {% if products.has_next %}
                <a href="?{% param_replace request page=products.next_page_number %}" class="page-link">
                    <i class="fas fa-angle-right"></i>
                </a>
                <a href="?{% param_replace request page=products.paginator.num_pages %}" class="page-link">
                    <i class="fas fa-angle-double-right"></i>
                </a>
            {% endif %}
        </div>
        {% endif %}
    </main>
</body>
</html>